﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using _10_HPBar.GameFrame.System;
using _10_HPBar.GameFrame;
using _10_HPBar.CharacterBase;
using _10_HPBar.GameFrame.Interface;
using static System.Formats.Asn1.AsnWriter;
using _10_HPBar.Items;

namespace _10_HPBar.Scene
{
    internal class SceneMain : IScene
    {
        public string Name => "SceneMain";

        Map map;
        InfoSystem info;
        DialogSystem dialog;
        CombatSystem combat;
        AnimeSystem anime;
        

        public Player player;
        public CollisionTest collision;

        public SceneMain()
        {
            KeyValuePair<string,string>[] paths = new KeyValuePair<string, string>[4];
            paths[0] = new KeyValuePair<string, string>("map0","../../../Map/map.csv");
            paths[0] = new KeyValuePair<string, string>("map1","../../../Map/map1.csv");
            paths[0] = new KeyValuePair<string, string>("map2","../../../Map/map2.csv");
            paths[0] = new KeyValuePair<string, string>("map3","../../../Map/map3.csv");
            info = new InfoSystem(24, 2, 10, 16);
            combat = new CombatSystem(40, 2, info);
            map = new Map(paths, 20, 20, 2, 2, this, info, combat);
            dialog = new DialogSystem(5, 24);
            collision = new CollisionTest();
            anime = new AnimeSystem(38,2);

            player = new Player("玩家", 100, 50, 10, 5,new Pos(), map, "玩", info, this, combat);
        }

        public void End()
        {

        }

        public void Init()
        {
            map.ChangeMap(0);
            player.AddInteractionAction(PlayerInteraction);
            dialog.overDialog += () => Character.IsDialoging = false;
            EventSystem.Instance.InvokeEvent("更改选项名称");
        }

        public void Input()
        {
            dialog.Input();
            combat.Input();
            player.Input();
            
            
            if (Game.input.IsKeyPressed(ConsoleKey.Escape))
            {
                Game.scene.ChangeSceneByName("SceneMenu");
            }
            if (Game.input.IsKeyPressed(ConsoleKey.F1))
            {
                anime.Play("Anime1");
            }
        }

        public void Render()
        {
            map.Render();
            info.Render();
            player.Render();
            dialog.Render();
            combat.Render();
            anime.Render();
            collision.gameObjects.ForEach(obj =>
            {
                obj.GetType().GetMethod("Render")?.Invoke(obj, null);
            });
        }

        public void Update()
        {
            dialog.Update();
            combat.Update();
            player.Update();
            collision.gameObjects.ForEach(obj =>
            {
                obj.GetType().GetMethod("Update")?.Invoke(obj, null);
            });
        }

        public void FixedUpdate()
        {

        }

        public GameObject? GetGameObject(List<GameObject> list, Pos pos)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Pos == pos)
                    return list[i];
            }
            return null;
        }

        void PlayerInteraction(GameObject obj)
        {
            // 如果玩家交互的点是宝箱
            //GameObject? t = GetGameObject(treasures, pos);
            //if (t != null)
            //{
            //    // 交互逻辑
            if(obj == null)return;
            dialog.StartDialog(((Treasure)obj).DialogId);
            Character.IsDialoging = true;

            //    // 对话选项逻辑
            dialog.selection1 += () =>
            {
                info.AddInfo($"打开了箱子{((Treasure)obj).index}");
                collision.Distroy(obj);
                //map.SetType(pos, Map.Type.Empty);
            };

            dialog.selection2 += () =>
            {
                info.AddInfo($"没打开箱子{((Treasure)obj).index}");
            };
        }
            //// 如果交互的是怪物
            //GameObject? e = GetGameObject(enemies, pos);
            //if (e != null)
            //{
            //    info.AddInfo($"碰到了怪物{((Enemy)e).Name}");
            //    // 打开战斗系统
            //    combat.StartCombat(player, (Enemy)e);
            //}
        
    }
}